package com.app.qbake.module.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.devilist.recyclerwheelpicker.TripleWheelPicker;
import com.devilist.recyclerwheelpicker.dialog.WheelPicker;
import com.wind.base.utils.ViewInflater;
import com.wind.data.mine.bean.Address;
import com.app.qbake.R;
import com.app.qbake.module.mine.layout.EditAddressLayout;
import com.wind.view.ValidateEditText;
import com.wind.view.ValidateTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class EditAddressFragment extends Fragment {


    private static final String ARGS_KEY_INFO = "args_key_info";
    @BindView(R.id.et_contact_content)
    ValidateEditText et_contact_content;
    @BindView(R.id.et_phone_content)
    ValidateEditText et_phone_content;
    @BindView(R.id.et_address_content)
    ValidateTextView et_address_content;
    @BindView(R.id.et_detail_content)
    ValidateEditText et_detail_content;

    EditAddressLayout mEditAddressLayout;

    private Address mAddress;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View itemView=inflater.inflate(R.layout.fragment_edit_address,container,false);
        return itemView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);
        mAddress= (Address) getArguments().getSerializable(ARGS_KEY_INFO);
        mEditAddressLayout=new EditAddressLayout(getActivity());
        if (mAddress!=null){
            ViewInflater.inflateText(et_contact_content,mAddress.getName());
            ViewInflater.inflateText(et_phone_content,mAddress.getPhone());
            String province=mAddress.getProvince();
            String city=mAddress.getCity();
            String area=mAddress.getArea();
            String val;
            if (TextUtils.isEmpty(area)){
                val=province+"-"+city;
            }else {
                val=province+"-"+city+"-"+area;
            }
            ViewInflater.inflateText(et_address_content,val);
            ViewInflater.inflateText(et_detail_content,mAddress.getAddress());
        }
    }

    @OnClick(R.id.et_address_content)
    public void onViewClick(View view){
        WheelPicker picker = TripleWheelPicker.instance()
                .setGravity(Gravity.BOTTOM)
                .setResource(R.raw.picker_location_3)
                .setPickerListener(new WheelPicker.OnPickerListener() {
                    @Override
                    public void onPickResult(String tag, String... result) {


                        String province;
                        String city;
                        String area;
                        String abode;

                        province = result[0];
                        city = result[1];
                        area = result[2];
                        if (TextUtils.isEmpty(area)){
                            abode = province + "-" + city;
                        }else {
                            abode = province + "-" + city + "-" + area;
                        }

                        et_address_content.setText(abode);
                    }
                })
                .build();
        picker.show(getChildFragmentManager());
    }


    public void update(){
        if (validate()){
            Address address=new Address();
            address.setName(getText(et_contact_content));
            address.setPhone(getText(et_phone_content));
            address.setAddress(getText(et_detail_content));


            String [] addr=getText(et_address_content).split("-");
            String province;
            String city;
            String area;
            if (addr.length==2){
                province=addr[0];
                city=addr[1];
                area="";
            }else {
                province=addr[0];
                city=addr[1];
                area=addr[2];
            }
            address.setProvince(province);
            address.setCity(city);
            address.setArea(area);
            if (mAddress!=null){
                address.setId(mAddress.getId());
                address.setIs_default(mAddress.getIs_default());
                mEditAddressLayout.edit(address);
            }else {
                mEditAddressLayout.add(address);
            }

        }
    }

    private boolean validate() {

        return et_contact_content.validate("请输入联系人")
                && et_phone_content.validate("请输入手机号码")
                && et_address_content.validate("请选择地区")
                && et_detail_content.validate("请输入详细地址");

    }

    private String getText(TextView tv){
        return tv.getText().toString().trim();
    }



    public static EditAddressFragment getInstance(Address address){
        EditAddressFragment f=new EditAddressFragment();
        Bundle args=new Bundle();
        args.putSerializable(ARGS_KEY_INFO,address);
        f.setArguments(args);
        return f;
    }
}
